package yannan.voiplock4ble.utils;

import android.content.Context;
import android.text.format.Time;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/* 
 * @author Msquirrel
 */
public class TimeUtils {

    public static void outLog(String msg) {

        boolean isShow = true;
        if (isShow) {
            Log.e("陈杰宇", msg);
        }
    }

    /*
    Time time = new Time("GMT+8");
        time.setToNow();
        int year = time.year;
        int month = time.month;
        int day = time.monthDay;
        int minute = time.minute;
        int hour = time.hour;
        int sec = time.second;
     */

    public static int getYear() {
        Time time = new Time();
        time.setToNow();
        return time.year;
    }

    public static int getMonth() {
        Time time = new Time();
        time.setToNow();
        return time.month + 1;
    }

    public static int getDay() {
        Time time = new Time();
        time.setToNow();
        return time.monthDay;
    }

    public static int getHour() {
        Time time = new Time();
        time.setToNow();
        return time.hour;
    }

    public static int getMinute() {
        Time time = new Time();
        time.setToNow();
        return time.minute;
    }

    public static int getSecond() {
        Time time = new Time();
        time.setToNow();
        return time.second;
    }

    /**
     * 读取assets中的文件
     *
     * @param ctx
     * @param fileName
     * @return
     * @date 2016-4-29 下午3:15:22
     * @author 陈杰宇
     */
    public static String getFromAssets(Context ctx, String fileName) {
        try {
            InputStreamReader inputReader = new InputStreamReader(ctx.getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            while ((line = bufReader.readLine()) != null)
                Result += line;
            return Result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }

    // private SimpleDateFormat sf ;
    /* 获取系统时间 格式为："yyyy/MM/dd " */
    public static String getCurrentDate() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }


    public static String getCurrentDateCN() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }


    /**
     * 获取当前时间 格式为时分
     *
     * @return
     */
    public static String getTimeHHmm() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
        return sf.format(d);
    }

    /**
     * 将国际格式的时间转换成中国格式的时间
     *
     * @param time
     * @return
     */
    public static String getDateIN2CN(String time) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        String dateCN = "";
        try {
            date = sf.parse(time);
            dateCN = sf2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateCN;
    }

    /**
     * 根据时间获取当前星期天
     *
     * @param time
     * @return
     */
    public static String getWeekByDateStr(String time) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        c.setTime(date);
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "星期" + mWay;
    }

    /* 时间戳转换成字符串 */
    public static String getDateToString(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    public static String getDateToStringYMDHM(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sf.format(d);
    }

    public static String getDateToStringYMDHMs(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    public static String getDateToString(Date time) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(time);
    }

    public static String getlong2Day(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
        return sf.format(d);
    }

    /* 时间戳转换成字符串 */
    public static String getDateToString1(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sf.format(d);
    }

    /* 时间戳转换成字符串 */
    public static String getDateToString2(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /* 将字符串转为时间戳 */
    public static long getStringToDate(String time) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static long getNowTimeLong() {
        Date date = new Date();
        return date.getTime();
    }

    //获取月初
    public static String getStartDate() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
        return sf.format(d) + "-01";
    }

    /* 将字符串转为时间戳 */
    public static long getStringToDate2(String time) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 获取明天的日期
     *
     * @return
     */
    public static String getDataTomorrow() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        Date tomorrow = cal.getTime();
        return getDateToString(tomorrow);
    }

    /**
     * 获取昨天的日期
     *
     * @return
     */
    public static String getYestarday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -1);
        Date tomorrow = cal.getTime();
        return getDateToString(tomorrow);
    }


    public static Date getYestardayDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -1);
        Date tomorrow = cal.getTime();
        return tomorrow;
    }

    /**
     * 获取当前星期天
     *
     * @return
     */
    public static String getDataWeekCN() {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "星期" + mWay;
    }

    /**
     * 将字符串的时间转换成时间戳
     *
     * @param dateStr
     * @return
     */
    public static long getStr2Long(String dateStr) {
        long time = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        try {
            Date date = sdf.parse(dateStr);
            time = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 返回日期
     *
     * @return
     */
    public static String getNowTimeHMS() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sf.format(d);
    }

    /**
     * 返回日期
     *
     * @return
     */
    public static String getNowTimeyMdHMS() {
        Date d = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    /**
     * 将字符串的时间转换成时间戳
     *
     * @param dateStr
     * @return
     */
    public static long getStr2Time1(String dateStr) {
        long time = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        try {
            Date date = sdf.parse(dateStr);
            time = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 将现有的时间减一
     *
     * @param time
     * @return
     */
    public static String getMinishTime(String time) {
        long lTime = getStr2Time1(time);
        return getDateToString2(lTime - 1000);
    }
}
// DateUtils.getCurrentDate(); //获取系统当前时间
// DateUtils.getDateToString(时间戳); //时间戳转为时间格式
// DateUtils.getStringToDate("时间格式");//时间格式转为时间戳
